<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="管线位置" prop="pipelineLocation">
        <el-input
          v-model="queryParams.pipelineLocation"
          placeholder="请输入管线位置"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="管径(mm)" prop="pipeDiameter">-->
<!--        <el-input-->
<!--          v-model="queryParams.pipeDiameter"-->
<!--          placeholder="请输入管径(mm)"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="长度(m)" prop="length">-->
<!--        <el-input-->
<!--          v-model="queryParams.length"-->
<!--          placeholder="请输入长度(m)"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="体积(L)" prop="volume">-->
<!--        <el-input-->
<!--          v-model="queryParams.volume"-->
<!--          placeholder="请输入体积(L)"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="管控措施" prop="controlMeasures">-->
<!--        <el-input-->
<!--          v-model="queryParams.controlMeasures"-->
<!--          placeholder="请输入管控措施"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用" prop="remarkSpare">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare"-->
<!--          placeholder="请输入备用"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用1" prop="remarkSpare1">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare1"-->
<!--          placeholder="请输入备用1"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用2" prop="remarkSpare2">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare2"-->
<!--          placeholder="请输入备用2"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ledger:pipeline:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ledger:pipeline:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ledger:pipeline:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['ledger:pipeline:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pipelineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="管线位置" align="center" prop="pipelineLocation" :formatter="pipelineLocationFormat" />
      <el-table-column label="管径(mm)" align="center" prop="pipeDiameter" :formatter="pipeDiameterFormat" />
      <el-table-column label="长度(m)" align="center" prop="length" :formatter="lengthFormat" />
      <el-table-column label="体积(L)" align="center" prop="volume" :formatter="volumeFormat" />
      <el-table-column label="管控措施" align="center" prop="controlMeasures" :formatter="controlMeasuresFormat" />
      <el-table-column label="备注" align="center" prop="remark" :formatter="remarkFormat" />
<!--      <el-table-column label="备用" align="center" prop="remarkSpare" :formatter="remarkSpareFormat" />-->
<!--      <el-table-column label="备用1" align="center" prop="remarkSpare1" :formatter="remarkSpare1Format" />-->
<!--      <el-table-column label="备用2" align="center" prop="remarkSpare2" :formatter="remarkSpare2Format" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ledger:pipeline:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ledger:pipeline:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改胀压管道清单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="管线位置" prop="pipelineLocation">
          <el-input v-model="form.pipelineLocation" placeholder="请输入管线位置" />
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="管径(mm)" prop="pipeDiameter">
          <el-input-number v-model="form.pipeDiameter" placeholder="请输入管径(mm)" style="width: 100%"/>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="长度(m)" prop="length">
          <el-input-number v-model="form.length" placeholder="请输入长度(m)" style="width: 100%"/>
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="体积(L)" prop="volume">
          <el-input-number v-model="form.volume" placeholder="请输入体积(L)" style="width: 100%"/>
        </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="管控措施" prop="controlMeasures">
          <el-input v-model="form.controlMeasures" placeholder="请输入管控措施" type="textarea"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea"/>
        </el-form-item>
<!--        <el-form-item label="备用" prop="remarkSpare">-->
<!--          <el-input v-model="form.remarkSpare" placeholder="请输入备用" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用1" prop="remarkSpare1">-->
<!--          <el-input v-model="form.remarkSpare1" placeholder="请输入备用1" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用2" prop="remarkSpare2">-->
<!--          <el-input v-model="form.remarkSpare2" placeholder="请输入备用2" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPipeline, getPipeline, delPipeline, addPipeline, updatePipeline, exportPipeline } from "@/api/ledger/pipeline";

export default {
  name: "Pipeline",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 胀压管道清单表格数据
      pipelineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pipelineLocation: null,
        pipeDiameter: null,
        length: null,
        volume: null,
        controlMeasures: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pipelineLocation: [
          { required: true, message: "管线位置不能为空", trigger: "blur" }
        ],
        pipeDiameter: [
          { required: true, message: "管径(mm)不能为空", trigger: "blur" }
        ],
        length: [
          { required: true, message: "长度(m)不能为空", trigger: "blur" }
        ],
        volume: [
          { required: true, message: "体积(L)不能为空", trigger: "blur" }
        ],
        controlMeasures: [
          { required: true, message: "管控措施不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();

  },
  methods: {
    /** 查询胀压管道清单列表 */
    getList() {
      this.loading = true;
      listPipeline(this.queryParams).then(response => {
        this.pipelineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pipelineLocation: null,
        pipeDiameter: null,
        length: null,
        volume: null,
        controlMeasures: null,
        remark: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加胀压管道清单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPipeline(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改胀压管道清单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePipeline(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPipeline(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除胀压管道清单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPipeline(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有胀压管道清单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          // return exportPipeline(queryParams);
        this.download('/ledger/pipeline/export', {
          ...this.queryParams
        }, `胀压管道清单.xlsx`)
        this.exportLoading = false;
        }).then(response => {
          // this.download(response.msg);
          // this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
